
--[[


--]]


local uuid = require("utils.uuid")
local ltui = require("ltui")
local label       = ltui.label
local button      = ltui.button
local application = ltui.application
local event       = ltui.event
local rect        = ltui.rect
local boxdialog   = ltui.boxdialog
local textdialog  = ltui.textdialog
local inputdialog = ltui.inputdialog
local panel       = ltui.panel


---------------------------------------------------------
-- class
---------------------------------------------------------
local ScreneBase = class("ScreneBase")

function ScreneBase:ctor(name)

    self.app = application()
    self.ui_close = false --是否关闭ui

    self.windows = {} --window列表

    self:register()
end


function ScreneBase:register()

end

function ScreneBase:onStart()
    local app = self.app
    app.init = function()
        self:initUI()
    end
    app.loop = function()
        self:loop()
    end

    app:run()
end

function ScreneBase:onClose()
    self.app:exit()
end


--创建一个label
function ScreneBase:createText(text, x, y)
    --默认白色
    local lb = label:new("label", rect({0, 0, 50, 1}), text):textattr_set("white")
    if x and y then
        local bounds = lb:bounds()
        bounds:move2(x, y)
    end
    self.app:insert(lb)
    return lb
end

function ScreneBase:createSprite(x, y, color)
    color = color or "red"
    local uid = uuid.random()
    local pn = panel:new("panel"..uid, rect({0, 0, 2, 1}))
    pn:background_set(color)
    self.app:insert(pn)
    if x and y then
        local bounds = pn:bounds()
        bounds:move2(x, y)
    end
    return pn
end


function ScreneBase:initUI()
    application.init(self.app, "app")
    self.app:background_set("blue")

    --new("client.login", self.app)
    new("client.game", self.app)

    local text = "close"
    local btn = button:new("btn", rect:new(self.app:width() - 10, 0, #text, 2), text, function()
        self.ui_close = true
        self.app:exit()
    end)
    btn:background_set("red")
    self.app:insert(btn)
end

function ScreneBase:onDraw()
    application.on_draw(self.app)
end

function ScreneBase:onEvent(e)
    if e.type < event.ev_max then
        --local str = 'type: ' ..
        --        tostring(e.type) ..
        --        '; name: ' ..
        --        tostring(e.key_name or e.btn_name) ..
        --        '; code: ' ..
        --        tostring(e.key_code or e.x) ..
        --        '; meta: ' ..
        --        tostring(e.key_code or e.y)
        --local bounds = self.lb:bounds()
        --bounds:resize(#str, 1)
        --self.lb:bounds_set(bounds)
        --self.lb:text_set(str)
        --local bounds = self.spr:bounds()
        --if e.key_code == 115 then --s
        --    bounds:move(0, 1)
        --    self.spr:invalidate(true)
        --elseif e.key_code == 119 then --w
        --    bounds:move(0, -1)
        --    self.spr:invalidate(true)
        --elseif e.key_code == 100 then --d
        --    bounds:move(1, 0)
        --    self.spr:invalidate(true)
        --elseif e.key_code == 97 then --a
        --    bounds:move(-1, 0)
        --    self.spr:invalidate(true)
        --end
    end
    application.on_event(self.app, e)
end

function ScreneBase:loop()
    local e = nil
    local sleep = true
    local app = self.app
    g.fork(function()
        while true do
            -- get the current event
            e = app:event()
            -- do event
            if e then
                self:onEvent(e)
                sleep = false
            else
                -- do idle event
                self:onEvent(event.idle())
                sleep = true
            end
            -- quit?
            if e and (event.is_command(e, "cm_quit")  )then
                break
            end
            if self.ui_close then
                break
            end
            -- resize views
            if app:state("resize") then
                self:onResize()
            end
            -- draw views
            self:onDraw()
            -- refresh views
            if app:state("refresh") then
                self:onRefresh()
            end
            -- wait some time, 50ms
            if sleep then
                g.sleep(5)
            end
        end
    end)
end

--窗口大小改变
function ScreneBase:onResize()

    application.on_resize(self.app)
end

--刷新了
function ScreneBase:onRefresh()

    application.on_refresh(self.app)
end

function ScreneBase:addChild(window)

end















return ScreneBase